package com.pb.b2b.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.pb.b2b.common.dao.impl.BaseDaoImpl;
import com.pb.b2b.dao.LanguageDAO;
import com.pb.b2b.model.Language;
import com.pb.b2b.model.enums.Status;

@Repository
public class LanguageDAOImpl extends BaseDaoImpl implements LanguageDAO {

	@Autowired
	SessionFactory sessionFactory;

	@Override
	public Language getDefaultLanguage() {

		Criteria criteria = sessionFactory.openSession().createCriteria(Language.class);
		criteria.add(Restrictions.eq("active", "X"));

		if (criteria.list() == null || criteria.list().size() == 0)
			return null;
		return (Language) criteria.list().get(0);
	}

	@Override
	public List<Language> getLanguages(Status status) {
		Criteria criteria = sessionFactory.openSession().createCriteria(Language.class);
		criteria.add(Restrictions.eq("status", status));
		criteria.addOrder(Order.desc("active"));

		return criteria.list();
	}

}
